package com.ruoyi.web.controller.mes;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.domain.mes.MesPrintTechnology;
import com.ruoyi.web.domain.mes.MesPrintTechnologyDetail;
import com.ruoyi.web.service.MesPrintTechnologyDetailService;
import com.ruoyi.web.service.MesPrintTechnologyService;
import com.ruoyi.web.vo.MesMachine;
import com.ruoyi.web.vo.ProduceCovering;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/printTechnology")

public class MesPrintTechnologyController extends BaseController {

    @Resource
    private MesPrintTechnologyService mesPrintTechnologyService;
    @Resource
    private MesPrintTechnologyDetailService mesPrintTechnologyDetailService;

    //查询list
    @PostMapping(value = "/list")
    public TableDataInfo list(@RequestBody Map<String, String> params) {
        startPage(params);

        List<MesPrintTechnology> data = mesPrintTechnologyService
                .list(new QueryWrapper<MesPrintTechnology>());

        //Map<String, MesPrintTechnology> map = data.stream().collect(Collectors.toMap(MesPrintTechnology::getId, obj -> obj));
        List<String> listId = data.stream().map(t -> t.getId()).collect(Collectors.toList());
        Map<String, List<MesPrintTechnologyDetail>> map = new HashMap<>();

        List<MesPrintTechnologyDetail> mesPrintTechnologyDetailList = new ArrayList<>();
        if (listId.size() > 0) {
            mesPrintTechnologyDetailList = mesPrintTechnologyDetailService
                    .list(new QueryWrapper<MesPrintTechnologyDetail>().lambda().in(MesPrintTechnologyDetail::getTechnologyId, listId));
        }

        for (MesPrintTechnology mesPrintTechnology : data) {
            List<MesPrintTechnologyDetail> list = mesPrintTechnologyDetailList.stream().filter(t -> t.getTechnologyId().equals(mesPrintTechnology.getId())).sorted(Comparator.comparing(MesPrintTechnologyDetail::getProcedureOrder)).collect(Collectors.toList());
            mesPrintTechnology.setMesPrintTechnologyDetailList(list);
        }

//        for (MesPrintTechnology mesPrintTechnology : data) {
//            List<MesPrintTechnologyDetail> mesPrintTechnologyDetailList = mesPrintTechnologyDetailService
//                    .list(new QueryWrapper<MesPrintTechnologyDetail>().lambda().eq(MesPrintTechnologyDetail::getTechnologyId, mesPrintTechnology.getId())
//                            .orderByAsc(MesPrintTechnologyDetail::getProcedureOrder));
//            mesPrintTechnology.setMesPrintTechnologyDetailList(mesPrintTechnologyDetailList);
//        }
        TableDataInfo list = getDataTable(data);
        return list;
    }

    //根据id查询详情
    @PostMapping(value = "/getById")
    public AjaxResult getById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        MesPrintTechnology mesPrintTechnology = mesPrintTechnologyService.getById(id);
        List<MesPrintTechnologyDetail> mesPrintTechnologyDetailList = mesPrintTechnologyDetailService
                .list(new QueryWrapper<MesPrintTechnologyDetail>().lambda().eq(MesPrintTechnologyDetail::getTechnologyId, id)
                        .orderByAsc(MesPrintTechnologyDetail::getProcedureOrder));
        mesPrintTechnology.setMesPrintTechnologyDetailList(mesPrintTechnologyDetailList);
        return AjaxResult.success("查询成功", mesPrintTechnology);
    }

    //新增
    @PostMapping(value = "/add")
    @Transactional
    public AjaxResult add(@RequestBody MesPrintTechnology mesPrintTechnology) {
        if (mesPrintTechnology.getId() == null) {
            mesPrintTechnology.setId(UUID.randomUUID().toString().replace("-", ""));
        }
        mesPrintTechnologyService.saveOrUpdate(mesPrintTechnology);

        for (MesPrintTechnologyDetail mesPrintTechnologyDetail : mesPrintTechnology.getMesPrintTechnologyDetailList()) {
            String name = mesPrintTechnologyDetail.getProcedureName();

            if (mesPrintTechnologyDetail.getId() == null) {
                mesPrintTechnologyDetail.setId(UUID.randomUUID().toString().replace("-", ""));
            }
            mesPrintTechnologyDetail.setTechnologyId(mesPrintTechnology.getId());
        }

        if (mesPrintTechnology.getMesPrintTechnologyDetailList() != null && mesPrintTechnology.getMesPrintTechnologyDetailList().size() != 0) {

            mesPrintTechnologyDetailService.remove(new QueryWrapper<MesPrintTechnologyDetail>()
                    .lambda().eq(MesPrintTechnologyDetail::getTechnologyId, mesPrintTechnology.getId()));
            mesPrintTechnologyDetailService.saveBatch(mesPrintTechnology.getMesPrintTechnologyDetailList());
        }
        return AjaxResult.success("添加成功", mesPrintTechnology.getId());
    }

    //删除
    @PostMapping(value = "/deleteById")
    public AjaxResult deleteById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        mesPrintTechnologyService.removeById(id);
        mesPrintTechnologyDetailService.remove(new QueryWrapper<MesPrintTechnologyDetail>().lambda().eq(MesPrintTechnologyDetail::getTechnologyId, id));
        return AjaxResult.success("删除成功");
    }

}
